import { Component,Injectable} from '@angular/core';
import { UserService} from './user.service';
import { User} from '../../services/model/user';
import { UserType} from '../../services/model/userType';
import { PopService} from 'dolphinng';
@Component({
    selector: 'user',
    templateUrl: './user.component.html',
    styleUrls: ['./user.component.less'],
    providers:[UserService]
})
@Injectable()
export class UserComponent {
  tableData:any[];
  page:{
    pageIndex:number,
    pageSize:number,
    count:number
  };
  searchParams:{
    username?:number,
    name?:string,
    userType?:number,
  };
  userTypes:UserType[];
  userTypeOptions:UserType[];
  name:number;


  modalAdd={
    visible:false,
    submitted:false,
    data:new User(),
    userTypeOptions:[]
  };
  modalEdit={
    visible:false,
    submitted:false,
    data:new User(),
    userTypeOptions:[]
  };
  constructor(private userSvc:UserService,private pop:PopService){
    this.resetParams();
    this.getUserTypes();
    this.page={
      pageIndex:0,
      pageSize:10,
      count:0
    };
  }
  getUserTypes(){
    this.userSvc.getUserTypes()
      .then((data)=>{
        if(data.length) {
          this.userTypes = data;
          let userType=new UserType();
          userType.userType=-1;
          userType.userTypeDescription='全部';

          this.userTypeOptions=[userType].concat(data);
        }
        console.log(this.userTypeOptions);
      })
  }
  search(){
    this.page.pageIndex=0;
    this.page.count=1;
    this.tableData=[];
    this.query();
  }
  query(){
    let params={
      username:this.searchParams.username,
      name:this.searchParams.name,
      userType:this.searchParams.userType,
      pageIndex:this.page.pageIndex,
      pageSize:this.page.pageSize
    };
    this.userSvc.query(params)
      .then((res)=>{
        console.log(res);
        this.tableData=res.items;
        this.page.count=res.count;
      });
  }

  testUser(user:User):boolean{
    let regExp=/^[0-9]+$/;
      if(!user.username){
        this.pop.error({
          text:'请输入用户名（学号）！'
        });
        return false;
      }else if(!regExp.test(user.username+'')){
        this.pop.error({
          text:'学号只能是纯数字！'
        });
        return false;
      }else if(user.userType===-1){
        this.pop.error({
          text:'请选择用户类型！'
        });
        return false;
      }
    return true;
  }
  showAddModal(){
    this.modalAdd.submitted=false;
    this.modalAdd.data=new User();
    this.modalAdd.userTypeOptions=this.userTypes;
    this.modalAdd.data.userType=this.modalAdd.userTypeOptions[0].userType;
    this.modalAdd.visible=true;
  }
  closeAddModal(){
    this.modalAdd.visible=false;
  }
  add(){
    if(this.testUser(this.modalAdd.data)){
      this.modalAdd.submitted=true;
      this.userSvc.add(this.modalAdd.data)
        .then((data)=>{
          this.modalAdd.submitted=false;
          this.modalAdd.visible=false;
          if(data.status){
            this.pop.info({
              text:'添加成功！'
            });
            this.query();
          }else{
            this.pop.info({
              title:'添加失败！',
              text:data.message
            });
          }
        });
    }
  }
  showEditModal(user:User){
    this.modalEdit.submitted=false;
    this.modalEdit.data=new User();
    this.modalEdit.userTypeOptions=this.userTypes;
    this.modalEdit.data.userId=user.userId;
    this.modalEdit.data.userType=user.userType;
    this.modalEdit.data.username=user.username;
    this.modalEdit.data.password=user.password;
    this.modalEdit.visible=true;
  }
  closeEditModal(){
    this.modalEdit.visible=false;
  }
  update(){
    if(this.testUser(this.modalEdit.data)){
      this.modalEdit.submitted=true;
      this.userSvc.update(this.modalEdit.data)
        .then((data)=>{
          this.modalEdit.submitted=false;
          this.modalEdit.visible=false;
          if(data.status){
            this.pop.info({
              text:'修改成功！'
            });
            this.query();
          }else{
            this.pop.info({
              title:'修改失败！',
              text:data.message
            });
          }
        });
    }
  }
  remove(id:number){
    this.pop.confirm({
      text:'确定要删除这个用户？',
      closeOnConfirm:false,
      showLoaderOnConfirm:true
    }).onConfirm(()=>{
      this.userSvc.remove(id)
        .then((data)=>{
          if(data.status){
            this.pop.info({
              text:'删除成功！'
            });
            this.query();
          }else{
            this.pop.info({
              title:'删除失败！',
              text:data.message
            });
          }
        });
    })
  }
  resetParams(){
    this.searchParams={
      userType:-1
    }
  }
}
